<?php
/*
 *	--------------------------------------------------------------------------------------------------------------------------
 *	Program			: HTML Form Field Generator, PHP Class Library
 *	Version			: 1.0.0
 *	Files			: htmlform.inc.php, htmlform_base.inc.php, htmlform_exc.inc.php
 *	Author			: Lasantha Samarakoon
 *	Date released	: Monday, September 21, 2009
 *	Email			: lasn1987@gmail.com
 *	Licence			: http://www.gnu.org/licenses/gpl.txt
 *	--------------------------------------------------------------------------------------------------------------------------
 *
 *	This program is a freeware, which falls under GNU Genral Public Licence.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	You can modify this program, without any permission from the author. But be kind enough to send the updated version to the
 *	author through the above mentioned Email address.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	Documentation:
 *			Please refer the test.php file for hints on the usage of this class library.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	************************************* PROUD TO BE A SRI LANKAN...!!! ******************************************************
 */
 
require_once 'htmlform_exc.inc.php';

interface IGeneratable {	

	// this function is used to generate html coding.
	function generate();
}

// this is the base class for all form fields except checkbox and radio
abstract class FormField {

	protected $properties 	= array('name' => null, 'id' => null, 'className' => null, 'disabled' => false);
	
	// constructor
	function __construct($name = null, $id = null, $className = null, $disabled = false) {
		
		$this->name 		= $name;
		$this->id 			= $id;
		$this->className 	= $className;
		$this->disabled 	= $disabled;
	}
	
	function __get($key) {
		
		if(array_key_exists($key, $this->properties))
			return $this->properties[$key];
	}
	
	function __set($key, $val) {
		
		if($key == 'disabled')
			if(!is_bool($val))
				throw new InvalidParameterException("boolean (true or false)");	
				
		if(array_key_exists($key, $this->properties))
			$this->properties[$key] = $val;
	}
}
// this is the base class for text, password and hidden fields
abstract class TextFieldBase extends FormField {
	
	// constructor
	function __construct($name = null, $id = null, $value = null, $className = null, $disabled = false) {

	// invoke super class constructor
		parent::__construct($name, $id, $className, $disabled);
		
		$this->properties['value'] = $value;
	}
	
	function generate_field($type) {
		
		$r = "<input type=\"$type\"";
		// check for available attributes
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		if($this->value 	!= null) $r .= " value=\"{$this->value}\"";
		if($this->disabled 	== true) $r .= " disabled=\"disabled\"";
		
		$r .= " />\n";
		// return html code
		return $r;
	}
}
// this is the base class for checkbox and radio
abstract class CheckboxBase extends FormField {
	
	//constructor
	function __construct($name = null, $id = null, $checked = false, $className = null, $disabled = false) {
		// invoke super class constructor
		parent::__construct($name, $id, $className, $disabled);
		
		$this->properties['checked'] = false;
		
		$this->checked = $checked;
	}
	
	function __set($key, $val) {
		
		if($key == 'checked')
			if(!is_bool($val))
				throw new InvalidParameterException("boolean (true or false)");
		
		parent::__set($key, $val);
	}
		
	function generate_field($type) {
		
		$r = "<input type=\"$type\"";
		// check for available attributes
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		if($this->checked 	== true) $r .= " checked=\"checked\"";
		if($this->disabled	== true) $r .= " disabled=\"disabled\"";
		
		$r .= " />\n";
		// return htm code
		return $r;
	}
}

// this is the base class for all buttons
abstract class ButtonBase extends FormField {

	// constructor 
	function __construct($name = null, $id = null, $value = null, $className = null, $disabled = false) {
		// invoke super class constructor
		parent::__construct($name, $id, $className, $disabled);
		
		$this->properties['value'] = $value;
		
	}
	
	function generate_field($type) {
		
		$r = "<button type=\"$type\"";
		// check for available attributes
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		if($this->disabled 	== true) $r .= " disabled=\"disabled\"";
		
		$r .= ">{$this->value}</button>\n";
		// return html code
		return $r;
	}
}
?>